library(leebounds)

#F3

bounds_df_f3 = 
  analysis_df %>%
  left_join(., analysis_df_subset %>% select(ROWNUM_ID, java_index_f3_1, non_java_index_f3_1, 
                                             regional_index_f3_1, religious_index_f3_1, national_index_f3_1, corruption_index_f3_1)) %>%
  filter(abs(forcing_f3) < 5) %>%
  mutate(selection = (!(is.na(q1_consent)))*1)

lee_func_f3 = function(df, var, label) {
  var = enquo(var)
  
  leedata =
    df %>%
    mutate(outcome = !!var) %>%
    dplyr::select(selection, outcome, treat = f3) %>%
    filter(!(is.na(outcome) & selection == 1))
  
  bounds = GetBounds(leebounds(leedata))
  
  return(c(label, bounds))
  
}


java_index_f3 = lee_func_f3(bounds_df_f3, var = java_index_f3_1, label = "Java Pref. (Java)")
non_java_index_f3 = lee_func_f3(bounds_df_f3, var = non_java_index_f3_1, label = "Java Pref. (non-Java)")
regional_index_f3 = lee_func_f3(bounds_df_f3, var = regional_index_f3_1, label = "Regional Pref.")
religious_index_f3 = lee_func_f3(bounds_df_f3, var = religious_index_f3_1, label = "Religious Intolerance")
national_index_f3 = lee_func_f3(bounds_df_f3, var = national_index_f3_1, label = "Nationalism")
corruption_index_f3 = lee_func_f3(bounds_df_f3, var = corruption_index_f3_1, label = "Corruption")


#T1

bounds_df = 
  analysis_df %>%
  left_join(., analysis_df_subset %>% select(ROWNUM_ID, java_index_f1_1, non_java_index_f1_1, 
                                             regional_index_f1_1, religious_index_f1_1, national_index_f1_1, corruption_index_f1_1)) %>%
  filter(abs(forcing_t1) < 1) %>%
  mutate(selection = (!(is.na(q1_consent)))*1)


lee_func = function(df, var, label) {
  var = enquo(var)
  
  leedata =
    df %>%
    mutate(outcome = !!var) %>%
    dplyr::select(selection, outcome, treat = t1) %>%
    filter(!(is.na(outcome) & selection == 1))
  
  bounds = GetBounds(leebounds(leedata))
  
  return(c(label, bounds))
  
}


java_index = lee_func(bounds_df, var = java_index_f1_1, label = "Java Pref. (Java)")
non_java_index = lee_func(bounds_df, var = non_java_index_f1_1, label = "Java Pref. (non-Java)")
regional_index = lee_func(bounds_df, var = regional_index_f1_1, label = "Regional Pref.")
religious_index = lee_func(bounds_df, var = religious_index_f1_1, label = "Religious Intolerance")
national_index = lee_func(bounds_df, var = national_index_f1_1, label = "Nationalism")
corruption_index = lee_func(bounds_df, var = corruption_index_f1_1, label = "Corruption")


mods = list(java_index_f3, java_index, non_java_index_f3, non_java_index, regional_index_f3, regional_index,
            religious_index_f3, religious_index, national_index_f3, national_index, corruption_index_f3, corruption_index)


unlist(mods) %>%
  matrix(ncol = 6, byrow = T) %>%
  .[,-4] %>%
  data.frame() %>%
  set_colnames(c("outcome", "lb_f3", "ub_f3", "lb_t1", "ub_t1")) %>%
  mutate(lb_f3 = round(as.numeric(lb_f3), 3)) %>%
  mutate(ub_f3 = round(as.numeric(ub_f3), 3)) %>%
  mutate(f3_n = 251079) %>%
  mutate(lb_t1 = round(as.numeric(lb_t1), 3)) %>%
  mutate(ub_t1 = round(as.numeric(ub_t1), 3)) %>%
  mutate(t1_n = 44073) %>%
  dplyr::select(outcome, lb_f3, ub_f3,f3_n, lb_t1, ub_t1, t1_n) %>%
  kbl(., 
      format = "latex",
      caption = "Lee Bounds", 
      booktabs = T,
      escape = F,
      linesep = "",
      align = "lcccccc",
      col.names = c("", "Lower", "Upper", "N", "Lower", "Upper", "N")) %>%
  kable_styling() %>% 
  add_header_above(c(" " = 1, "Effect of SKD Failure" = 3, "Effect of Passing SKB" = 3)) %>%
  #column_spec(column = 1, width = "2in") %>%
  as.character() %>%
  str_replace(., "\\\\begin\\{table\\}", "\\\\begin{table}[!htbp]") %>%
  cat(., file = "./BKN survey/apsr_kuipers_replication_file/_4_outputs/tables/table_a3.tex")

getwd()

